<div class="box">

<!-- end box / title -->


<div class="table">
  <base href="<?php echo base_url() ?>" />

  <table id="records" class="showMatcode" width="100%">
    <thead> </thead>
    <tbody> </tbody>
  </table>

</div>

<!-- update form in dialog box -->
<div id="updateDialog" title="Update">
    <div>
        <form action="" method="post">
            <p>
               <label for="item_group_id">Item Group ID:</label>
               <div class="input">
    <select name="item_group_id" id="item_group_id">
      <?php
        $dt = $this->mitem->getValueFromTable();
                                foreach($dt as $row){
        echo'<option value="'. $row->item_group_id .'" '. ($data['item_group_id'] == $row->item_group_id ?"SELECTED":"") .'>'. $row->item_group_name .'</option>';
        }
      ?>
    </select>
        </div>
          </p>
      
    <p>
               <label for="item_category_id" width=200>Code ISIS:</label>
               <div class="input">
        <select name="item_category_id" id="item_category_id">
            <option value="0" SELECTED>Pilih Category</option>
            <?
                $dt = $this->mitem->getValueFromTableIsis();
                foreach($dt as $row){
                echo'<option value="'. $row->category_id .'" '. ($data['category_id'] == $row->category_id ?"SELECTED":"") .'>'. $row->category_name .'</option>';
                }
            ?>
        </select>
          </div>
         </p>

         <p>
               <label for="item_category_id" width=200>Item Category ID:</label>
               <div class="input">
        <select name="dbr_id" id="item_category_id">
            <option value="0" SELECTED>Pilih Category</option>
            <?
                $dt = $this->mitem->getValueFromTableCategory();
                foreach($dt as $row){
                echo'<option value="'. $row->dbr_id .'" '. ($data['dbr_id'] == $row->dbr_id ?"SELECTED":"") .'>'. $row->dbr_name .'</option>';
                }
            ?>
        </select>
          </div>
         </p>


          <p>
          <label for="default_price">Harga Retail:</label>
           <div class="input">
            <input type="text" id="default_price" name="default_price" />
          </div>
            </p>
        
            <!--<p>
               <label for="username">Username:</label>
               <input type="text" id="username" name="username" />
            </p>
            <p>
               <label for="email">Email:</label>
               <input type="text" id="email" name="email" />
            </p>
            -->
            <input type="hidden" id="userId" name="id" />
        </form>
    </div>
</div>

<!-- message dialog box -->
<div id="msgDialog"><p></p></div>

<script type="text/javascript">


var readUrlOutlet   = 'index.php/item/read_matcode/',
delHref,
updateHref,
updateId;

$(document).ready( function() {

  showDataMatcode();

}); //end document ready



// Jquery Data Tabel 18 Feb 2013 projack89
function showDataMatcode() {
  $('.showMatcode').dataTable({
    "bJQueryUI": true, // aktifkan tampilan tabel JQueryUI
    "sAjaxSource": readUrlOutlet, //data source
    "sAjaxDataProp": "aaData", //variabel yg menampung data table
    "bServerSide": true, //serverside processing true
    "bProcessing": true, // tampilkan 'processing...' status saat data dipanggil
    //"sPaginationType": 'fullNumbers',
    "bPaginate": true,
    "aoColumns":[ //tentukan kolom pd tabel yg akan menampung value dari server
      {"mDataProp": "imap.item_code",   "sTitle": "Code Item"},
    {"mDataProp": "imap.item_name",      "sTitle": "Nama Item"},
       
     {"mDataProp": "imap.default_price", "sTitle": "Default Price"},
    {"mDataProp": "grp.item_group_name", "sTitle": "Category Name"},
    {"mDataProp": "isis.isisname", "sTitle": "Isis Name"}
    ],

    "fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) 
    {
     //$(nRow).find('td').eq(-1).after('<a href="'+approveUrlNew+'/'+aaData[0]+'">Approve</a> |  <a href="'+updateUrlNew+'/'+aaData[0]+'">Edit</a> | <a href="'+delUrlNew+'/'+aaData[0]+'">Delete</a>'); //buat link action 
      //$(nRow).find('td').eq(0).before('<td>'+(parseInt(iDisplayIndex,10)+1+parseInt(dis))+'</td>'); 
     $(nRow).find('td').eq(-1).after('<td><a id="'+aaData[0]+'" onclick="update(this.id)" class="updateBtn" target="_blank">Update</a></td>'); //buat link action 
      return nRow;
    }, 

    "fnServerData": function ( sSource, aoData, fnCallback ) { 
      $.getJSON( sSource, aoData, function (json) { 

      dis=json['iDisplayStart'];
      fnCallback(json);
      });
    }  
  });

  //$('.showOutlet thead tr').find('th').eq(0).before('<th class="ui-state-default">No</th>');
  $('.showMatcode thead tr').find('th').eq(-1).after('<th class="ui-state-default">Proses</th>');
}

$( '#msgDialog' ).dialog({
        autoOpen: false,
        
        buttons: {
            'Ok': function() {
                $( this ).dialog( 'close' );
                window.location.reload();
            }
        }
    });

$( '#updateDialog' ).dialog({

        autoOpen: false,
        buttons: {
            'Update': function() {
                $( '#ajaxLoadAni' ).fadeIn( 'slow' );
                //alert('update');
                $( this ).dialog( 'close' );
                
                $.ajax({
                    url: '<?php echo base_url() ?>index.php/act_item/update',
                    type: 'POST',
        dataType: 'json',
                    data: $( '#updateDialog form' ).serialize(),
                    
                    success: function( response ) {
                        //var obj = JSON.parse(response.message);
      //alert(response.message);
                        $( '#msgDialog > p' ).html( response.message );
      //$( '#msgDialog > p' ).html( response );
                        $( '#msgDialog' ).dialog( 'option', 'title', 'Success' ).dialog( 'open' );
                        
                        $( '#ajaxLoadAni' ).fadeOut( 'slow' );
                        
                        //--- update row in table with new values ---
                        
      var item_group_id = $( 'tr#' + updateId + ' td' )[ 2 ];
                        //var username = $( 'tr#' + updateId + ' td' )[ 2 ];
                        //var email = $( 'tr#' + updateId + ' td' )[ 3 ];
                       
                        $( item_group_id ).html( response.groupname);
                        //$( username ).html( $( '#username' ).val() );
                        //$( email ).html( $( '#email' ).val() );
                        
                        //--- clear form ---
                        $( '#updateDialog form input' ).val( '' );
                        
                    } //end success
                    
                }); //end ajax()
            },
            
            'Cancel': function() {
                $( this ).dialog( 'close' );
            }
        },
        width: '350px'
    }); //end update dialog
  function update(id){
  var id =id;
$( 'a.updateBtn').click(function() {

        updateHref = '<?php echo base_url() ?>index.php/act_item/update';
        updateId = id;
        

        
        $( '#ajaxLoadAni' ).fadeIn( 'slow' );
        
        $.ajax({
            url: '<?php echo base_url() ?>index.php/act_item/getById/' + updateId,
            dataType: 'json',
            
            success: function( response ) {
        $( '#item_id' ).val( response.item_id );
                $( '#item_name' ).val( response.item_name );
        $( '#item_code' ).val( response.item_code );
                $( '#item_group_id' ).val( response.item_group_id );
                $( '#item_category_id' ).val( response.item_category_id );
                $( '#item_group_name' ).val( response.item_group_name );
                $( '#default_price' ).val(response.default_price);               
                
                $( '#ajaxLoadAni' ).fadeOut( 'slow' );
                
                //--- assign id to hidden field ---
                $( '#userId' ).val( updateId );
                
                

                 
                
                $( '#updateDialog' ).dialog( 'open' );
            }
        });
    
    });
    }
</script>

</div>